﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Forms;

namespace GameSPK.Content
{
    /// <summary>
    /// Interaction logic for frmSection4Content.xaml
    /// </summary>
    public partial class frmSection4Content : Window
    {
        General cuocthichon;
        int phanthi = 4;
        int socauhoi;
        string filevideo;
        public MultiEvent EventPhanthi;
        int eventdanhap;
        int index;
        int soevent;
        MultiEvent[] phanNhieudukien;
        private void reset()
        {
            filevideo = null;
            EventPhanthi = new MultiEvent();
            eventdanhap = 0;
            index = 0;
            soevent = 0;
            this.txtCauHoi.Text = "";
            this.txtNumDuKien.Text = "0";
            txtDuKien.Text = "";
            txtDapan.Text = "";
            this.cboLoaiDuKien.SelectedIndex = 0;
            this.mEVideo.Source = null;
            
        }
        public frmSection4Content()
        {
            InitializeComponent();
            //mEVideo.Source = new Uri(@"C:\Users\HXPHUNG\Downloads\Video\video.FLV");
            mEVideo.LoadedBehavior = MediaState.Manual;
            EventPhanthi = new MultiEvent();
            //mEVideo.Play();
        }

        private void cboChuDe_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (isFillData() == false)
            {
                System.Windows.Forms.MessageBox.Show("Hãy Nhập Đầy Đủ Dữ Liệu");
                return;
            }

            EventPhanthi.cauhoi = txtCauHoi.Text.Trim();
            EventPhanthi.chuDe = (ChuDe)cboChuDe.SelectedItem;
            EventPhanthi.dapan = txtDapan.Text.Trim();
            EventPhanthi.sodukien = soevent;
            /*
            String folder = "event";
            String filename = Helper.GenerateKey() + ".xml";
            EventPhanthi.SaveXML(folder + "/" + filename);
            if (CauHoiDAO.luuCauHoi(MainWindow.cuocthiluachon, 4,EventPhanthi.chuDe, folder + "/" + filename, "", true))
            {
                reset();
            }
             * */
            phanNhieudukien[cboCauHoi.SelectedIndex] = EventPhanthi;
            indexphanthi++;
            System.Windows.Forms.MessageBox.Show("Nhập câu hỏi thành công");
            //reset();
            //this.cboCauHoi.SelectedIndex = 0;
        }
        private bool isFillData()
        {
            if (this.txtCauHoi.Text.Trim() == "")
                return false;
            if (this.txtDapan.Text.Trim() == "")
                return false;
            if (EventPhanthi.lstDuKien.Count < int.Parse(txtNumDuKien.Text))
                return false;
            return true;
        }
        int indexphanthi;
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            DBTroChoiDataContext db = new DBTroChoiDataContext();
            try
            {
                cuocthichon = MainWindow.getCuocThiChon();
                if (cuocthichon == null)
                    return;
                var chude = from cd in db.ChuDes
                            select cd;
                foreach (ChuDe cde in chude)
                {
                    cboChuDe.Items.Add(cde);
                }
                cboChuDe.SelectedIndex = 0;
                socauhoi = CauHoiDAO.tongCauHoiPhanThi(phanthi, cuocthichon.MaCuocChoi);
                phanNhieudukien = new MultiEvent[socauhoi];
                indexphanthi = 0;
                for (int i = 0; i < socauhoi; i++)
                {
                    cboCauHoi.Items.Add((i + 1) + "");

                }
                cboCauHoi.SelectedIndex = 0;
            }
            catch
            {
            }
        }

        private void btnAddChuDe_Click(object sender, RoutedEventArgs e)
        {
            frmAddChuDe themChude = new frmAddChuDe();
            themChude.ShowDialog();
            DBTroChoiDataContext db = new DBTroChoiDataContext();
            cboChuDe.Items.Clear();
            try
            {
                var chude = from cd in db.ChuDes
                            select cd;
                foreach (ChuDe cde in chude)
                {
                    cboChuDe.Items.Add(cde);
                }
                cboChuDe.SelectedIndex = 0;
            }
            catch
            {
            }
        }

        private void cboLoaiDuKien_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cboLoaiDuKien.SelectedIndex == 0)
            {
                this.txtDuKien.Visibility = System.Windows.Visibility.Visible;
                this.lblImage.Visibility = System.Windows.Visibility.Hidden;
                this.mEVideo.Visibility = System.Windows.Visibility.Hidden;
                btnChonFile.Visibility = System.Windows.Visibility.Hidden;
            }
            else if (cboLoaiDuKien.SelectedIndex == 1)
            {
                this.txtDuKien.Visibility = System.Windows.Visibility.Hidden;
                this.lblImage.Visibility = System.Windows.Visibility.Hidden;
                this.mEVideo.Visibility = System.Windows.Visibility.Visible;
                btnChonFile.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                this.txtDuKien.Visibility = System.Windows.Visibility.Hidden;
                this.lblImage.Visibility = System.Windows.Visibility.Hidden;
                this.mEVideo.Visibility = System.Windows.Visibility.Visible;
                btnChonFile.Visibility = System.Windows.Visibility.Visible;
            }
        }
        bool ischonfile = false;
        private void btnChonFile_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openDialog = new OpenFileDialog();
            ImageBrush myBrush = new ImageBrush();
            openDialog.Filter = "Image Files (*.bmp, *.jpg, *.gif, *.png, *.flv, *.avi, *.mp4, *.wmv)|*.bmp;*.jpg;*.gif; *.png; *.flv; *.avi; *.mp4; *.wmv";
            if (openDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                filevideo = openDialog.FileName;

                System.IO.File.Copy(filevideo, @"media\" + openDialog.SafeFileName, true);
                filevideo = @"media\" + openDialog.SafeFileName;
                ischonfile = true;
                this.mEVideo.Source = new Uri(filevideo, UriKind.RelativeOrAbsolute);
                mEVideo.Play();
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            soevent = int.Parse(txtNumDuKien.Text);
            if (eventdanhap < soevent)
            {
                eventdanhap++;

                if (index < eventdanhap)
                {
                    mEVideo.Source = null;
                    if (cboLoaiDuKien.SelectedIndex == 0)
                    {
                        if (txtDuKien.Text.Trim() == "")
                        {
                            System.Windows.Forms.MessageBox.Show("Nhập Nội Dung Dữ Kiện");
                            return;
                        }
                    }
                    else
                    {
                        if (ischonfile == false)
                        {
                            System.Windows.Forms.MessageBox.Show("Chọn File Dữ Kiện");
                            return;
                        }
                    }


                    CEvent cEvent = new CEvent();
                    if (cboLoaiDuKien.SelectedIndex == 0)
                    {
                        cEvent.eloaiDuKien = loaidukien.info;
                        cEvent.NoiDung = this.txtDuKien.Text.Trim();
                    }
                    else
                    {
                        cEvent.eloaiDuKien = loaidukien.media;
                        cEvent.NoiDung = filevideo;
                        ischonfile = false;
                    }
                    EventPhanthi[index] = cEvent;
                    index++;
                    if (index >= soevent)
                    {
                        index = 0;
                        if (EventPhanthi[index].eloaiDuKien == loaidukien.info)
                        {
                            cboLoaiDuKien.SelectedIndex = 0;
                            txtDuKien.Text = EventPhanthi[index].NoiDung;
                        }
                        else
                        {
                            cboLoaiDuKien.SelectedIndex = 1;
                            filevideo = EventPhanthi[index].NoiDung;
                            this.mEVideo.Source = new Uri(filevideo,  UriKind.RelativeOrAbsolute);
                            mEVideo.Play();

                            ischonfile = true;
                        }
                        
                    }
                    else
                    {
                        ischonfile = false;
                        txtDuKien.Text = "";
                        
                    }
                }
            }
            else
            {
                if (index >= soevent)
                {
                    index = 0;
                    if (EventPhanthi[index].eloaiDuKien == loaidukien.info)
                    {
                        cboLoaiDuKien.SelectedIndex = 0;
                        txtDuKien.Text = EventPhanthi[index].NoiDung;
                    }
                    else
                    {
                        cboLoaiDuKien.SelectedIndex = 1;
                        filevideo = EventPhanthi[index].NoiDung;
                        this.mEVideo.Source = new Uri(filevideo, UriKind.RelativeOrAbsolute);
                        mEVideo.Play();

                        ischonfile = true;
                    }
                }
                else
                {
                    if (cboLoaiDuKien.SelectedIndex == 0)
                    {
                        EventPhanthi[index].eloaiDuKien = loaidukien.info;
                        EventPhanthi[index].NoiDung = this.txtDuKien.Text.Trim();
                    }
                    else
                    {
                        EventPhanthi[index].eloaiDuKien = loaidukien.media;
                        EventPhanthi[index].NoiDung = filevideo;
                        ischonfile = false;
                    }
                    index++;
                    if (index < soevent)
                    {
                        if (EventPhanthi[index].eloaiDuKien == loaidukien.info)
                        {
                            cboLoaiDuKien.SelectedIndex = 0;
                            txtDuKien.Text = EventPhanthi[index].NoiDung;
                        }
                        else
                        {
                            cboLoaiDuKien.SelectedIndex = 1;
                            filevideo = EventPhanthi[index].NoiDung;
                            this.mEVideo.Source = new Uri(filevideo, UriKind.RelativeOrAbsolute);
                            mEVideo.Play();

                            ischonfile = true;
                        }
                    }
                    else
                    {
                        index = 0;
                        if (EventPhanthi[index].eloaiDuKien == loaidukien.info)
                        {
                            cboLoaiDuKien.SelectedIndex = 0;
                            txtDuKien.Text = EventPhanthi[index].NoiDung;
                        }
                        else
                        {
                            cboLoaiDuKien.SelectedIndex = 1;
                            filevideo = EventPhanthi[index].NoiDung;
                            this.mEVideo.Source = new Uri(filevideo, UriKind.RelativeOrAbsolute);
                            mEVideo.Play();

                            ischonfile = true;
                        }
                    }
                }


            }
        }

        private void cboCauHoi_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (phanNhieudukien[cboCauHoi.SelectedIndex] == null)
            {
                reset();
            }
            else
            {
                this.txtCauHoi.Text = phanNhieudukien[cboCauHoi.SelectedIndex].cauhoi;
                this.txtDapan.Text = phanNhieudukien[cboCauHoi.SelectedIndex].dapan;
                this.txtNumDuKien.Text = phanNhieudukien[cboCauHoi.SelectedIndex].sodukien + "";
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnSaveDB_Click(object sender, RoutedEventArgs e)
        {
            bool flag = true;
            for (int i = 0; i < socauhoi; i++)
            {
                if (phanNhieudukien[i] == null)
                {
                    flag = false;
                    break;
                }
            }
            if (!flag)
            {
                System.Windows.Forms.MessageBox.Show("Nhập đủ số lượng câu hỏi");
                return;
            }
            String folder = "event";
            String key = Helper.GenerateKey();
            try
            {
                for (int i = 0; i < socauhoi; i++)
                {
                    String filename = key+ "" + i + ".xml";
                    EventPhanthi.SaveXML(folder + "/" + filename);
                    if (!CauHoiDAO.luuCauHoi(MainWindow.cuocthiluachon, 4, EventPhanthi.chuDe, folder + "/" + filename, "",i, true))
                    {
                        System.Windows.Forms.MessageBox.Show("Error");
                        return;
                    }
                }
                this.Close();
            }
            catch
            {

            }
            
        }

      
    }
}
